package com.sheep.jucdemo.part2.part2_12;

/**
 * @Date 2022/4/14 22:05
 * @Created by sheep
 */
public class Test2_12_3 {

    public boolean updateEntry(long id){
        boolean result = false;
        int retryNum = 5;
        while (retryNum>0){
            // （1）使用乐观锁获取指定记录
            EntryObject entry = query("select * from table1 where id = #{id}",id);
            // （2）修改记录内容，version字段不能被修改
            String name = generatorName(entry);
            entry.setName(name);
            // .....其他修改逻辑

            // （3）update操作
            int count = update("update table1 set name=#{name},age=#{age} version=#{version}+1 where id = #{id} and version=#{version}",entry);

            if (count == 1){
                result = true;
                break;
            }
            retryNum--;

        }

        return result;

    }

    // 以下为操作数据库行为或具体业务行为，防止编译报错返回null替代

    private int update(String s, EntryObject entry) {
        return 0;
    }

    private EntryObject query(String s, long id) {
        return null;
    }

    private String generatorName(EntryObject entry) {
        return null;
    }
}
